package leetcodev1.链表;

public class LeetCode83 {

    //删除有序链表中重复删除
    public ListNode deleteDuplicates(ListNode head) {
        //只增加一个虚拟节点即可
        ListNode index = head, virtualNode = new ListNode(Integer.MIN_VALUE), retIndex = virtualNode;
        while (index != null) {
            //跳过重复元素
            //只保留不重复元素（当前元素!=队尾元素则加入）
            if (retIndex.val != index.val) {
                retIndex.next = new ListNode(index.val);
                retIndex = retIndex.next;
            }

            index = index.next;
        }
        return virtualNode.next;
    }
}

class Answer {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }

        //保留第一个出现的节点
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.val == cur.next.val) {
                cur.next = cur.next.next;//原地删除
            } else {
                cur = cur.next;//向下执行
            }
        }

        return head;
    }

}
